package com.hp.bon.sgw.wh.channel.invoices.bizSlot;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hp.bon.sgw.wh.channel.common.BizConsts;
import com.hp.bon.sgw.wh.channel.common.BizParam;
import com.hp.bon.sgw.wh.channel.common.BizResult;
import com.hp.bon.sgw.wh.channel.invoices.BizSlot;

public class InvoiceInformationGetSlot implements BizSlot {
    private static final Logger _log = LoggerFactory.getLogger(InvoiceInformationGetSlot.class);

    @Override
    public BizResult doBiz(BizParam bizParam) {
        BizResult bizResult = new BizResult();


            //查询开票信息API
            _log.info("start INVOICEINFORMATION_GET......");

            String serialNumber = bizParam.getParamString(BizConsts.KEY_INVOICEINFORMATION_GET_SERIALNUMBER);
            String lanId = bizParam.getParamString(BizConsts.KEY_INVOICE_LANID);
            String type = bizParam.getParamString(BizConsts.KEY_INVOICEINFORMATION_GET_TYPE);
            String webserviceUrl = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);


            webserviceUrl += "?" + BizConsts.KEY_INVOICEINFORMATION_GET_SERIALNUMBER + "=" + serialNumber;
            webserviceUrl += "&" + BizConsts.KEY_INVOICE_LANID + "=" + lanId;
            webserviceUrl += "&" + BizConsts.KEY_INVOICEINFORMATION_GET_TYPE + "=" + type;

//            String resp = getWebserviceStr(webserviceUrl,bizResult);
            String resp = HttpClientUtils.getHttpStr(webserviceUrl,bizParam);
            if (resp.equals("")) {
                bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
                resp = HttpClientUtils.getErrorJsonStr(BizConsts.STATUS_ACOUNT_API_FAIL, "", "", "");
            }else {
                bizResult.setCode(BizConsts.STATUS_ACOUNT_API_SUCCESS);
            }
            bizResult.setHttpBody(resp);
            _log.info("Responding to external messages====:{}",resp);



        return bizResult;
    }

//    private static String getWebserviceStr(String webserviceUrl,BizResult bizParam) {
//        _log.info("===========webservice url===============" + webserviceUrl);
//        String resp = "";
//        _log.info("===========webservice url===============" + webserviceUrl);
//        //HttpGet httpGet = new HttpGet(webserviceUrl);
//        HttpClient client = new HttpClient();
//
//        GetMethod getMethod = new GetMethod(webserviceUrl);
//        getMethod.setRequestHeader("Content-Type",bizParam.getParamString(BizConsts.RQUEST_HEADER_CONTENT_TYPE));
//        getMethod.setRequestHeader("X-APP-ID",bizParam.getParamString(BizConsts.RQUEST_HEADER_X_APP_KEY));
//        getMethod.setRequestHeader("X-APP-KEY",bizParam.getParamString(BizConsts.RQUEST_HEADER_X_APP_KEY));
//        getMethod.setRequestHeader("X-CTG-Request-ID",bizParam.getParamString(BizConsts.RQUEST_HEADER_X_CTG_REQUEST_ID));
//        getMethod.setRequestHeader("X-CTG-Region-ID",bizParam.getParamString(BizConsts.RQUEST_HEADER_X_CTG_REGION_ID));
//
//        // 设置post请求超时时间
//        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
//        getMethod.addRequestHeader("Content-Type", "application/json");
//        try {
//            client.executeMethod(getMethod);
//        } catch (HttpException e) {
//            _log.info("===========webservice call timeout===============" + webserviceUrl);
//            return "";
//        } catch (IOException e) {
//            _log.info("===========webservice call timeout session_id:");
//            return "";
//        }
//        try {
//            _log.info("===========完成webservice===============" + resp);
//            InputStream str = getMethod.getResponseBodyAsStream();
//            BufferedReader br = new BufferedReader(new InputStreamReader(str));
//            StringBuffer stringBuffer = new StringBuffer();
//            String result = "";
//            while ((result = br.readLine()) != null) {
//                stringBuffer.append(result);
//            }
//            resp = stringBuffer.toString();
//            //resp = post.getResponseBodyAsString();
//        } catch (IOException e) {
//            _log.info("===========webservice call timeout===============" + webserviceUrl);
//            return "";
//        }
//        return resp;
//    }


}
